/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package panel;

import database.DataEngine;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

/**
 *
 * @author daz
 */
public class DeleteCpPanel extends Screen {

    private JLabel cpNameLabel;
    private JTextField cpInputField;
    private JLabel cpIdLabel;
    private JTextField cpIdInputField;
    private JButton deleteCpButton;
    private ActionListener deleteCpListenner;
    private DataEngine dataEngine;
    private JLabel databaseIpLabel;
    private JTextField databaseIpInputField;
    private JLabel databaseUserLabel;
    private JTextField databaseUserInputField;
    private JLabel databasePassLabel;
    private JPasswordField databasePassInputField;

    public DeleteCpPanel(JFrame frame) {
        super(frame);
    }

    @Override
    void initValue() {
    }

    @Override
    void initComponent() {
        //cp line
        setLayout(null);
        int left = 10;
        int top = 20;
        int height = 20;
        int width = 100;
        int linePadding = 35;
        cpNameLabel = new JLabel("Tên CP :");
        cpNameLabel.setBounds(left + 30, top, 250, height);
        add(cpNameLabel);
        cpInputField = new JTextField();
        cpInputField.setBounds(left + 130, top, 150, height);
        add(cpInputField);
//        cpIdLabel = new JLabel("Id :");
//        cpIdLabel.setBounds(left + 300, top, 30, height);
//        add(cpIdLabel);
//        cpIdInputField = new JTextField();
//        cpIdInputField.setBounds(left + 330, top, 50, height);
//        add(cpIdInputField);
        // sysntax line
        // database adress  line
        top += linePadding;
        databaseIpLabel = new JLabel("Địa chỉ database :");
        databaseIpLabel.setBounds(left + 30, top, 250, height);
        add(databaseIpLabel);

        databaseIpInputField = new JTextField();
        databaseIpInputField.setBounds(left + 130, top, 150, height);
        databaseIpInputField.setText("202.92.4.22");
        add(databaseIpInputField);
        // database user line
        top += linePadding;
        databaseUserLabel = new JLabel("Database User:");
        databaseUserLabel.setBounds(left + 30, top, 250, height);
        add(databaseUserLabel);

        databaseUserInputField = new JTextField();
        databaseUserInputField.setText("gamepoker");
        databaseUserInputField.setBounds(left + 130, top, 150, height);
        add(databaseUserInputField);
        // database pass line
        top += linePadding;
        databasePassLabel = new JLabel("Database Password:");
        databasePassLabel.setBounds(left + 30, top, 250, height);
        add(databasePassLabel);

        databasePassInputField = new JPasswordField();
        databasePassInputField.setText("GaMe2o12");
        databasePassInputField.setBounds(left + 130, top, 150, height);
        add(databasePassInputField);
        // delete cp button
        top += linePadding;
        left += width;
        deleteCpButton = new JButton("Xóa cp");
        add(deleteCpButton);
        deleteCpButton.addActionListener(deleteCpListenner);
        deleteCpButton.setBounds(left, top, width - 30, height);
    }

    @Override
    void initListenner() {
        deleteCpListenner = new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                deleteCp();
            }
        };
    }

    private void deleteCp() {
        Thread thread = new Thread(new Runnable() {

            @Override
            public void run() {
                dataEngine = DataEngine.getInstance();
                dataEngine.setConfig(databaseIpInputField.getText(), 3306, databaseUserInputField.getText(), databasePassInputField.getText(), "poker_online", "poker_online");
                dataEngine.connectToDabatabase();
                dataEngine.deleteCp(cpInputField.getText());
                showDialog();
            }
        });
        thread.start();
    }

    private void showDialog() {
        JOptionPane.showMessageDialog(frame,
                "Đã xóa thành công cp " + cpInputField.getText());
    }
}
